package jalus.utils;

import jalus.pojo.Post;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

public class MyUtils {
	
	public static Date parseDateCCT(String dateStr, String format){
		return parseDateByTimeZone(dateStr, "CCT", new Date(), format);
	}
	
	/**
	 * 根据时区和date format解析日期
	 * @param dateStr
	 * @param timeZone
	 * @param defaultDate
	 * @param format
	 * @return
	 */
	public static Date parseDateByTimeZone(String dateStr, String timeZone, 
			Date defaultDate, String format){
		Date date;
		try {
			if(StringUtils.isNotEmpty(dateStr)){
				SimpleDateFormat sdf = new SimpleDateFormat(format);
				sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
				date = sdf.parse(dateStr);
			}else{
				date = defaultDate;
			}
		} catch (ParseException e) {
			date = defaultDate;
		}
		return date;
	}
	
	public static void processPost(Post post){
		if(post.isAuthorized()){
			post.setTitle("【此文章已隐藏】");
			post.setContent(" ");
		}
	}
	
	public static void processPostList(List<Post> postList) {
		if (postList != null) {
			for (Post post : postList) {
				processPost(post);
			}
		}
	}
	
	/**
	 * date to string
	 * @return
	 */
	public static String formatDateCCT(Date date, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		sdf.setTimeZone(TimeZone.getTimeZone("CCT"));
		return sdf.format(date);
	}
	
	/**
	 * 获得过去一天的时间
	 * @param date
	 */
	public static Date decreaseDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime();
	}
	
	/**
	 * 获得一天的起始时间  Timezone CCT
	 * @param date
	 */
	public static Date getDayBeginDateCCT(Date date) {
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CCT"));
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	
	/**
	 * 获得一天的结束时间 Timezone CCT
	 * @param date
	 */
	public static Date getDayEndDateCCT(Date date) {
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CCT"));
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获得一月的起始时间  Timezone CCT
	 * @param date
	 */
	public static Date getMonthBeginDateCCT(Date date) {
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CCT"));
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获得一月的结束时间  Timezone CCT
	 * @param date
	 */
	public static Date getMonthEndDateCCT(Date date) {
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CCT"));
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
}
